<template>
  <div class="picture">
    <a-tabs default-active-key="营业额" @change="callback" :activeKey="key">
      <a-tab-pane key="营业额" tab="营业额">
        <div ref="xian" class="an"></div>
      </a-tab-pane>
      <a-tab-pane key="单品销量" tab="单品销量">
        <div ref="yuan" class="an"></div>
      </a-tab-pane>
      <a-tab-pane key="订单量" tab="订单量"> 
        <div ref="zhu" class="an"></div>
      </a-tab-pane>
    </a-tabs>
  </div>
</template>

<script>
export default {
  data() {
    return {
      key: "营业额",
    };
  },
  mounted() {
    this.initEchart("xian");
  },
  methods: {
    callback(key) {
      this.key = key;
      if (key === "单品销量") {
        setTimeout(() => {
          this.initEchart("yuan");
        }, 0);
      } else if(key==="订单量") {
        setTimeout(() => {
          this.initEchart("zhu");
        }, 0);
      }
    },
    initEchart(char) {
      let Chart = this.$echarts.init(this.$refs[char]);

      let yuanoption = {
        title: {
          text: "菜品销量",
          left: "center",
        },
        tooltip: {
          trigger: "item",
        },
        legend: {
          orient: "vertical",
          left: "left",
        },
        series: [
          {
            name: "Access From",
            type: "pie",
            radius: "50%",
            data: [
              { value: 1048, name: "Search Engine" },
              { value: 735, name: "Direct" },
              { value: 580, name: "Email" },
              { value: 484, name: "Union Ads" },
              { value: 300, name: "Video Ads" },
            ],
            emphasis: {
              itemStyle: {
                shadowBlur: 10,
                shadowOffsetX: 0,
                shadowColor: "rgba(0, 0, 0, 0.5)",
              },
            },
          },
        ],
      };
      let xianoption = {
        xAxis: {
          type: "category",
          data: ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"],
        },
        yAxis: {
          type: "value",
        },
        series: [
          {
            data: [150, 230, 224, 218, 135, 147, 260],
            type: "line",
          },
        ],
      };
      let zhuoption = {
        xAxis: {
          type: "category",
          data: ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"],
        },
        yAxis: {
          type: "value",
        },
        series: [
          {
            data: [120, 200, 150, 80, 70, 110, 130],
            type: "bar",
          },
        ],
      };
      let option;
      if (char == "yuan") option = yuanoption;
      else if (char == "xian") option = xianoption;
      else option = zhuoption;
      option && Chart.setOption(option);
    },
  },
};
</script>

<style scoped>
.picture {
  padding: 0px 20px;
}
.an {
  width: 100%;
  height: 520px;
}
</style>
